package com.kolon.business.mOI.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIJudgeExecSchController")
public class MOIJudgeExecSchController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIJudgeExecSch.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		// 파라미터
		String execNm = ComUtil.nullChk(request.getParameter("execNm"));
		ModelAndView modelAndView = new ModelAndView();
		if (!"".equals(execNm)) {
			Map model = new HashMap();
			// 도메인
			String url = MsgUtil.getMessage("xml.url");
			// 회원정보 세션
			HttpSession httpSession = request.getSession();
			Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
			String userId = (String)userMap.get("userId");
			String userPw = (String)userMap.get("userPw");
			String cpyCD = (String)userMap.get("CompanyCD");
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/oi_suggestion.aspx?strQuery=Y&strGubun=Joiner&Joiner=").append(execNm);
			buffer.append("&companycd=").append(cpyCD);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("Table");
			Iterator itXml = xmlList.iterator();
			List mapList = new ArrayList();
			Map map = new HashMap();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("UserName", el.getChild("UserName").getText());
				map.put("DeptName", el.getChild("DeptName").getText());
				map.put("DeptCode", el.getChild("DeptCode").getText());
				map.put("TitleName", el.getChild("TitleName").getText());
				map.put("UserAccount", el.getChild("UserAccount").getText());
				map.put("CompanyCD", el.getChild("CompanyCD").getText());
				mapList.add(map);
			}		
			model.put("list",mapList);
			return new ModelAndView("jsonView", model);
		} else {
			return modelAndView;
		}
	}

}
